import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import { useUserInfo } from "./store/user";
import { createPinia } from "pinia";
// 1. 引入你需要的组件
import {
    Checkbox,
    CheckboxGroup,
    Tag,
    Cell,
    CellGroup,
    Stepper,
    Popup,
    Button,
    Swipe,
    SwipeItem,
    Search,
    Sidebar,
    SidebarItem,
    Grid,
    GridItem,
    Image as VanImage,
    NavBar,
    Icon,
    Tab,
    Tabs,
    SubmitBar,
    CouponCell,
    CouponList,
    AddressList,
    AddressEdit,
    Area,
    Uploader,
    Loading,
} from "vant";
// 2. 引入组件样式
import "vant/lib/index.css";

const app = createApp(App);
app.use(createPinia());
app.use(router);
app.mount("#app");
app.use(Button);
app.use(Swipe);
app.use(SwipeItem);
app.use(Search);
app.use(Sidebar);
app.use(SidebarItem);
app.use(Grid);
app.use(GridItem);
app.use(VanImage);
app.use(NavBar);
app.use(Icon);
app.use(Tab);
app.use(Tabs);
app.use(Popup);
app.use(Stepper);
app.use(Cell);
app.use(CellGroup);
app.use(Tag);
app.use(Checkbox);
app.use(CheckboxGroup);
app.use(SubmitBar);
app.use(CouponCell);
app.use(CouponList);
app.use(AddressList);
app.use(AddressEdit);
app.use(Area);
app.use(Uploader);
app.use(Loading);

const $store = useUserInfo();

router.beforeEach((to, from, next) => {
    let { isRouterAuth } = to.meta;
    let token = $store.$state.token;

    if (isRouterAuth) {
        if (token) {
            next();
        } else {
            next({ path: "/login" });
        }
    } else {
        next();
    }
});
